package com.frame.ar.db;

import com.frame.ar.kit.ObjKit;

/*******************************************
 * 作用描述：查询参数封装
 *******************************************
 * @author LRC
 * @createDate 2015-9-12
 * @version v1.0 
 * @updateRecord 格式：时间，作者，修改说明
 *******************************************
 * 
 */
public class QueryParamsAnd {
	/**
	 * 查询的表名 
	 */
	private String tableName;
	/**
	 * 查询的字段信息
	 */
	private String selectColumn;
	/**
	 * 排序字段
	 */
	private String order;
	/**
	 * 字段名
	 */
	private String[] colName;
	/**
	 * 字段值
	 */
	private Object[] value;
	/**
	 * 查询表达式
	 */
	private String[] expression;
	/**
	 * 记录参数个数
	 */
	private int n = 0;
	/**
	 * 最大容量
	 */
	private int maxN = 5;

	public int length(){
		return n;
	}
	
	public QueryParamsAnd(int paramsNum) {
		this.maxN = paramsNum;
		init();
	}

	public QueryParamsAnd() {
		init();
	}

	private void init() {
		colName = new String[maxN];
		value = new Object[maxN];
		expression = new String[maxN];
	}

	// 扩容
	private void addMaxN() {
		maxN += maxN/2;
		String[] _colName = new String[maxN];
		Object[] _value = new Object[maxN];
		String[] _expression = new String[maxN];
		System.arraycopy(colName, 0, _colName, 0, colName.length);
		System.arraycopy(value, 0, _value, 0, value.length);
		System.arraycopy(expression, 0, _expression, 0, expression.length);
		colName = _colName;
		value = _value;
		expression = _expression;
	}

	/*******************************************
	 * 功能描述：填充参数，返回this
	 *******************************************
	 * @createDate 2015-9-12 by LRC
	 * @param _colName
	 * @param _value
	 * @param _expression
	 * @return 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public QueryParamsAnd put(String _colName, String _expression,Object _value) {
		if (n == maxN) {
			addMaxN();
		}
		colName[n] = _colName;
		expression[n] = _expression;
		value[n] = _value;
		n++;
		return this;
	}
	/*******************************************
	 * 功能描述：向传入的sql字符串中追加查询过滤语句
	 *******************************************
	 * @createDate 2015-9-12 by LRC
	 * @param sql 
	 * @return 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public String getQueryString(String sql){
		if(sql==null||"".equals(sql.trim()))return null;
		//判断传入的sql中是否包含where如果没有包含则追加 where 1=1
		StringBuilder _sql = new StringBuilder(sql.indexOf("where")>0 ? sql : sql + " where 1=1 ");
		for(int i=0; i<this.n; i++){
			_sql.append(" and "+colName[i]+" " + expression[i] +" ? ");
		}
		return ObjKit.isNull(order) ? _sql.toString() : _sql.toString() + " order by " + order;
	}
	public String getQueryString(){
		String sql = selectColumn==null ? "select * " : "select "+selectColumn+" ";
		sql += " from "+tableName;
		return getQueryString(sql);
	}
	
	public void clear(){
		this.tableName=null;
		this.selectColumn=null;
		this.order=null;
		this.n = 0;
	}
	
	public QueryParamsAnd setTableName(String tableName){
		this.tableName = tableName;
		return this;
	}
	public QueryParamsAnd setOrder(String order){
		this.order = order;
		return this;
	} 
	
	public QueryParamsAnd setSelectColumn(String selectColumn){
		this.selectColumn = selectColumn;
		return this;
	} 
	
	public String getColName(int i) {
		if (i <= n)
			return colName[i];
		else
			return null;
	}
	
	public Object getValue(int i) {
		if (i <= n)
			return value[i];
		else
			return null;
	}
	
	public String getExpression(int i) {
		if (i <= n)
			return expression[i];
		else
			return null;
	}
	public String[] getColName() {
		String[] _colName = new String[n]; 
		System.arraycopy(colName, 0, _colName, 0, n);
		return _colName;
	}
	public Object[] getValue() {
		Object[] _value = new Object[n]; 
		System.arraycopy(value, 0, _value, 0, n);
		return _value;
	}
	public String[] getExpression() {
		String[] _expression = new String[n]; 
		System.arraycopy(expression, 0, _expression, 0, n);
		return _expression;
	}
	
}
